package com.isunland.managesystem.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.text.InputType;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.isunland.managesystem.R;
import com.isunland.managesystem.utils.MyStringUtil;

/**
 * 对象功能: 单行文本输入框
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: wangyanan
 * 创建时间: 2017/3/21 17:13
 */

public class SingleLineView extends RelativeLayout {
  public static final String EMPTY = "empty";
  public static final String CLEAR = "clear";
  public static final int TEXT = 0;
  public static final int PASSWORD = 2;
  public static final int DECIMAL = 1;
  public static final int NORMAL = 0;
  //private static final int MULTILINE = 1;
  public static final int NEXT = 2;

  //============================================================================
  private Drawable mLogoSrc;
  private int mColor;
  private String mHint;
  private boolean mIsEnable;
  private int mInputType;
  private int mType;
  private TextView tvTitle;
  EditText tvContent;
  private String mTitle;
  private String mContent;
  private ImageView ivLogo;

  public SingleLineView(Context context) {
    super(context);
  }

  public SingleLineView(Context context, AttributeSet attrs) {
    super(context, attrs);
    //加载视图的布局
    inflateView(context);
    //加载自定义属性

    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SingleLineView);
    mTitle = a.getString(R.styleable.SingleLineView_textTitle);
    mContent = a.getString(R.styleable.SingleLineView_textContent);
    mContent = TextUtils.isEmpty(mContent) ? EMPTY : mContent;
    mHint = a.getString(R.styleable.SingleLineView_hint);
    mHint = TextUtils.isEmpty(mHint) ? EMPTY : mHint;
    mColor = a.getColor(R.styleable.SingleLineView_textColor, 0);
    mInputType = a.getInt(R.styleable.SingleLineView_inputType, TEXT);
    mType = a.getInt(R.styleable.SingleLineView_type, NORMAL);
    mLogoSrc = a.getDrawable(R.styleable.SingleLineView_logoSrc);
    mIsEnable = a.getBoolean(R.styleable.SingleLineView_enable, true);
    //回收资源，这一句必须调用
    a.recycle();
  }

  public TextView getTvTitle() {
    return tvTitle;
  }

  public void clear() {
    tvContent.setText("");
  }

  public void setTvTitle(TextView tvTitle) {
    this.tvTitle = tvTitle;
  }

  public EditText getTvContent() {
    return tvContent;
  }

  public void setTvContent(EditText tvContent) {
    this.tvContent = tvContent;
  }

  public ImageView getIvLogo() {
    return ivLogo;
  }

  public void setIvLogo(ImageView ivLogo) {
    this.ivLogo = ivLogo;
  }

  void inflateView(Context context) {

    LayoutInflater.from(context).inflate(R.layout.view_my_input, this, true);
  }

  /**
   * 此方法会在所有的控件都从xml文件中加载完成后调用
   */
  @Override protected void onFinishInflate() {
    super.onFinishInflate();
    //获取子控件
    findView();
    initView();
  }

  public void initView() {
    //llHolder = (LinearLayout) findViewById(R.id.ll_holder);
    //将从资源文件中加载的属性设置给子控件
    setTextTitle(mTitle);
    setTextContent(mContent);

    //初始化的时候如果hint为空,代表没有设置改属性,这时候显示默认的hint.
    //选择提示"请选择",输入提示"请输入"
    setColor(mColor);
    //默认是启用的,设置禁用.
    setType(mType);
    setInputType(mInputType);
    setHint(mHint);
    setInputEnabled(mIsEnable);
    setSrc(mLogoSrc);
  }

  public void findView() {
    tvTitle = (TextView) findViewById(R.id.tv_title);
    tvContent = (EditText) findViewById(R.id.tv_content);
    ivLogo = (ImageView) findViewById(R.id.iv_logo);
  }

  public void setSrc(Drawable mLogoSrc) {
    if (mLogoSrc != null) {
      ivLogo.setImageDrawable(mLogoSrc);
    }
  }

  /**
   * 获取内容文字
   *
   * @return 内容
   */
  public String getTextContent() {
    return tvContent.getText().toString().trim();
  }

  /**
   * 设置内容文字
   */
  public void setTextContent(String text) {
    if (!MyStringUtil.equals(EMPTY, text)) {

      tvContent.setText(text);
    }
  }

  @SuppressWarnings("unused") public TextView getViewContent() {
    return tvContent;
  }

  public TextView getViewTitle() {
    return tvTitle;
  }

  /**
   * 获取标题文字
   *
   * @return 内容
   */
  public String getTextTitle() {
    return tvTitle.getText().toString().trim();
  }

  /**
   * 设置标题文字
   */
  public void setTextTitle(String text) {
    if (!TextUtils.isEmpty(text)) {
      tvTitle.setText(text);
    }
  }

  /**
   * 设置提示文字
   */
  public void setHint(String text) {
    if (MyStringUtil.isEmpty(text) || MyStringUtil.equals(CLEAR, text)) {
      tvContent.setHint("");
      return;
    }
    if (MyStringUtil.equals(EMPTY, text)) {
      tvContent.setHint(mType == NEXT ? R.string.hintSelect : R.string.hintInput);
      return;
    }
    tvContent.setHint(text);
  }

  private void setColor(int mColor) {
    if (mColor != 0) {
      tvTitle.setTextColor(mColor);
      tvContent.setTextColor(mColor);
    }
  }

  /**
   * 设置输入类型
   */
  public void setInputType(int type) {
    switch (type) {
      case TEXT:
        tvContent.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_MULTI_LINE);
        break;

      case DECIMAL:
        tvContent.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
        break;

      case PASSWORD:
        tvContent.setInputType(InputType.TYPE_CLASS_TEXT
            | InputType.TYPE_TEXT_FLAG_MULTI_LINE
            | InputType.TYPE_TEXT_VARIATION_PASSWORD);
        break;
    }
  }

  /**
   * 启用或禁用
   */
  public void setInputEnabled(boolean enable) {
    //禁用输入的时候,清除提示信息
    if (!enable) {
      tvContent.setHint("");
    }

    tvContent.setEnabled(enable);
  }

  /**
   * 设置按钮点击事件监听器
   */
  public void setOnClickContentListener(OnClickListener listener) {
    tvContent.setTag(getId());
    tvContent.setOnClickListener(listener);
  }

  public void setType(int type) {
    switch (type) {
      case NORMAL:
        //tvContent.setHint(getContext().getString(R.string.hintInput));
        tvContent.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_MULTI_LINE);
        tvContent.setFocusableInTouchMode(true);//选择的时候onClick点击两次响应要设置false来解决,输入的时候恢复true.
        tvContent.setCompoundDrawables(null, null, null, null);
        break;
      //case MULTILINE:
      //
      //    break;
      case NEXT:
        //tvContent.setHint(getContext().getString(R.string.hintSelect));
        tvContent.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);
        tvContent.setFocusableInTouchMode(false);//onClick点击两次响应的解决办法.
        @SuppressWarnings("all") Drawable drawable =
            getContext().getResources().getDrawable(R.drawable.next);
        /**要绘制图片边界来确定大小,否则不会显示.*/
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());//对图片进行压缩
        tvContent.setCompoundDrawablePadding(10);
        tvContent.setCompoundDrawables(null, null, drawable, null);

        break;
    }
  }

  /**
   * 隐藏并清空内容
   */
  public void hide() {
    //tvContent.setText("");
    setVisibility(View.GONE);
  }

  public boolean isEmptyContent() {

    return TextUtils.isEmpty(tvContent.getText().toString().trim());
  }
}
